﻿Imports System.Text
Imports System.IO.File


Public Class FormIO
    Dim IO_Input(33) As System.Windows.Forms.Button
    Dim IO_Output(33) As System.Windows.Forms.Button
    Dim TEXT_IN(33) As System.Windows.Forms.TextBox
    Dim TEXT_OUT(33) As System.Windows.Forms.TextBox
    Dim debugPath As String = My.Application.Info.DirectoryPath
    Dim filePath As String = debugPath & "/Tool/Lable.txt"

    Private Sub FormIO_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'IO INPUT
        IO_Input(0) = Button_IN0
        IO_Input(1) = Button_IN1
        IO_Input(2) = Button_IN2
        IO_Input(3) = Button_IN3
        IO_Input(4) = Button_IN4
        IO_Input(5) = Button_IN5
        IO_Input(6) = Button_IN6
        IO_Input(7) = Button_IN7
        IO_Input(8) = Button_IN8
        IO_Input(9) = Button_IN9
        IO_Input(10) = Button_IN10
        IO_Input(11) = Button_IN11
        IO_Input(12) = Button_IN12
        IO_Input(13) = Button_IN13
        IO_Input(14) = Button_IN14
        IO_Input(15) = Button_IN15
        IO_Input(16) = Button_IN16
        IO_Input(17) = Button_IN17
        IO_Input(18) = Button_IN18
        IO_Input(19) = Button_IN19
        IO_Input(20) = Button_IN20
        IO_Input(21) = Button_IN21
        IO_Input(22) = Button_IN22
        IO_Input(23) = Button_IN23
        IO_Input(24) = Button_IN24
        IO_Input(25) = Button_IN25
        IO_Input(26) = Button_IN26
        IO_Input(27) = Button_IN27
        IO_Input(28) = Button_IN28
        IO_Input(29) = Button_IN29
        IO_Input(30) = Button_IN30
        IO_Input(31) = Button_IN31
        'IO OUTPUT
        IO_Output(0) = Button_OUT0
        IO_Output(1) = Button_OUT1
        IO_Output(2) = Button_OUT2
        IO_Output(3) = Button_OUT3
        IO_Output(4) = Button_OUT4
        IO_Output(5) = Button_OUT5
        IO_Output(6) = Button_OUT6
        IO_Output(7) = Button_OUT7
        IO_Output(8) = Button_OUT8
        IO_Output(9) = Button_OUT9
        IO_Output(10) = Button_OUT10
        IO_Output(11) = Button_OUT11
        IO_Output(12) = Button_OUT12
        IO_Output(13) = Button_OUT13
        IO_Output(14) = Button_OUT14
        IO_Output(15) = Button_OUT15
        IO_Output(16) = Button_OUT16
        IO_Output(17) = Button_OUT17
        IO_Output(18) = Button_OUT18
        IO_Output(19) = Button_OUT19
        IO_Output(20) = Button_OUT20
        IO_Output(21) = Button_OUT21
        IO_Output(22) = Button_OUT22
        IO_Output(23) = Button_OUT23
        IO_Output(24) = Button_OUT24
        IO_Output(25) = Button_OUT25
        IO_Output(26) = Button_OUT26
        IO_Output(27) = Button_OUT27
        IO_Output(28) = Button_OUT28
        IO_Output(29) = Button_OUT29
        IO_Output(30) = Button_OUT30
        IO_Output(31) = Button_OUT31
        'TEXTBOX
        TEXT_IN(0) = TextBox_IN0
        TEXT_IN(1) = TextBox_IN1
        TEXT_IN(2) = TextBox_IN2
        TEXT_IN(3) = TextBox_IN3
        TEXT_IN(4) = TextBox_IN4
        TEXT_IN(5) = TextBox_IN5
        TEXT_IN(6) = TextBox_IN6
        TEXT_IN(7) = TextBox_IN7
        TEXT_IN(8) = TextBox_IN8
        TEXT_IN(9) = TextBox_IN9
        TEXT_IN(10) = TextBox_IN10
        TEXT_IN(11) = TextBox_IN11
        TEXT_IN(12) = TextBox_IN12
        TEXT_IN(13) = TextBox_IN13
        TEXT_IN(14) = TextBox_IN14
        TEXT_IN(15) = TextBox_IN15
        TEXT_IN(16) = TextBox_IN16
        TEXT_IN(17) = TextBox_IN17
        TEXT_IN(18) = TextBox_IN18
        TEXT_IN(19) = TextBox_IN19
        TEXT_IN(20) = TextBox_IN20
        TEXT_IN(21) = TextBox_IN21
        TEXT_IN(22) = TextBox_IN22
        TEXT_IN(23) = TextBox_IN23
        TEXT_IN(24) = TextBox_IN24
        TEXT_IN(25) = TextBox_IN25
        TEXT_IN(26) = TextBox_IN26
        TEXT_IN(27) = TextBox_IN27
        TEXT_IN(28) = TextBox_IN28
        TEXT_IN(29) = TextBox_IN29
        TEXT_IN(30) = TextBox_IN30
        TEXT_IN(31) = TextBox_IN31
        '
        TEXT_OUT(0) = TextBox_OUT0
        TEXT_OUT(1) = TextBox_OUT1
        TEXT_OUT(2) = TextBox_OUT2
        TEXT_OUT(3) = TextBox_OUT3
        TEXT_OUT(4) = TextBox_OUT4
        TEXT_OUT(5) = TextBox_OUT5
        TEXT_OUT(6) = TextBox_OUT6
        TEXT_OUT(7) = TextBox_OUT7
        TEXT_OUT(8) = TextBox_OUT8
        TEXT_OUT(9) = TextBox_OUT9
        TEXT_OUT(10) = TextBox_OUT10
        TEXT_OUT(11) = TextBox_OUT11
        TEXT_OUT(12) = TextBox_OUT12
        TEXT_OUT(13) = TextBox_OUT13
        TEXT_OUT(14) = TextBox_OUT14
        TEXT_OUT(15) = TextBox_OUT15
        TEXT_OUT(16) = TextBox_OUT16
        TEXT_OUT(17) = TextBox_OUT17
        TEXT_OUT(18) = TextBox_OUT18
        TEXT_OUT(19) = TextBox_OUT19
        TEXT_OUT(20) = TextBox_OUT20
        TEXT_OUT(21) = TextBox_OUT21
        TEXT_OUT(22) = TextBox_OUT22
        TEXT_OUT(23) = TextBox_OUT23
        TEXT_OUT(24) = TextBox_OUT24
        TEXT_OUT(25) = TextBox_OUT25
        TEXT_OUT(26) = TextBox_OUT26
        TEXT_OUT(27) = TextBox_OUT27
        TEXT_OUT(28) = TextBox_OUT28
        TEXT_OUT(29) = TextBox_OUT29
        TEXT_OUT(30) = TextBox_OUT30
        TEXT_OUT(31) = TextBox_OUT31
        IO_LableRead()
        Button1.Text = "修改标签"
        Timer1.Enabled = True
        Button_Enable("disable")

    End Sub

    Private Sub FormIO_Closing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
        Button_Enable("disable")
        Button_IOcontrol.Text = "开启IO控制"



        Timer1.Enabled = False
    End Sub

    Public Sub Button_Enable(ByVal str As String)
        Try
            If str = "disable" Then
                For i = 0 To 31
                    IO_Output(i).Enabled = False
                Next
            Else
                For i = 0 To 31
                    IO_Output(i).Enabled = True
                Next
            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Sub Button_Change(ByVal ButtonNO As Integer)
        If OutputIO_Value(ButtonNO) = 0 Then
            OutputIO_Value(ButtonNO) = 1
        Else
            OutputIO_Value(ButtonNO) = 0
        End If
    End Sub

    Private Function HXtoB(ByVal s As String)
        Dim value As String = "0000"
        If s = "0" Then
            value = "0000"
        End If
        If s = "1" Then
            value = "0001"
        End If
        If s = "2" Then
            value = "0010"
        End If
        If s = "3" Then
            value = "0011"
        End If
        If s = "4" Then
            value = "0100"
        End If
        If s = "5" Then
            value = "0101"
        End If
        If s = "6" Then
            value = "0110"
        End If
        If s = "7" Then
            value = "0111"
        End If
        If s = "8" Then
            value = "1000"
        End If
        If s = "9" Then
            value = "1001"
        End If
        If s = "A" Or s = "a" Then
            value = "1010"
        End If
        If s = "B" Or s = "b" Then
            value = "1011"
        End If
        If s = "C" Or s = "c" Then
            value = "1100"
        End If
        If s = "D" Or s = "d" Then
            value = "1101"
        End If
        If s = "E" Or s = "e" Then
            value = "1110"
        End If
        If s = "F" Or s = "f" Then
            value = "1111"
        End If
        Return value
    End Function
    Private Sub IO_LableRead()
        Dim file_Exist As Boolean
        Dim SS() As String
        file_Exist = Exists(filePath)
        If file_Exist = True Then
            SS = ReadAllLines(filePath, System.Text.Encoding.Default)
            If SS.Length > 63 Then
                For I = 0 To 31
                    TEXT_IN(I).Text = SS(I)
                    TEXT_IN(I).ReadOnly = True
                Next
                For I = 32 To 63
                    TEXT_OUT(I - 32).Text = SS(I)
                    TEXT_OUT(I - 32).ReadOnly = True
                Next
            End If
        End If

    End Sub


    Private Sub Button_OUT0_Click(sender As Object, e As EventArgs) Handles Button_OUT0.Click
        Button_Change(0)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT1_Click(sender As Object, e As EventArgs) Handles Button_OUT1.Click
        Button_Change(1)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT2_Click(sender As Object, e As EventArgs) Handles Button_OUT2.Click
        Button_Change(2)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT3_Click(sender As Object, e As EventArgs) Handles Button_OUT3.Click
        Button_Change(3)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT4_Click(sender As Object, e As EventArgs) Handles Button_OUT4.Click
        Button_Change(4)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT5_Click(sender As Object, e As EventArgs) Handles Button_OUT5.Click
        Button_Change(5)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT6_Click(sender As Object, e As EventArgs) Handles Button_OUT6.Click
        Button_Change(6)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT7_Click(sender As Object, e As EventArgs) Handles Button_OUT7.Click
        Button_Change(7)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT8_Click(sender As Object, e As EventArgs) Handles Button_OUT8.Click
        Button_Change(8)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT9_Click(sender As Object, e As EventArgs) Handles Button_OUT9.Click
        Button_Change(9)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT10_Click(sender As Object, e As EventArgs) Handles Button_OUT10.Click
        Button_Change(10)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT11_Click(sender As Object, e As EventArgs) Handles Button_OUT11.Click
        Button_Change(11)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT12_Click(sender As Object, e As EventArgs) Handles Button_OUT12.Click
        Button_Change(12)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT13_Click(sender As Object, e As EventArgs) Handles Button_OUT13.Click
        Button_Change(13)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT14_Click(sender As Object, e As EventArgs) Handles Button_OUT14.Click
        Button_Change(14)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT15_Click(sender As Object, e As EventArgs) Handles Button_OUT15.Click
        Button_Change(15)
        IO_SET(1, 0)
    End Sub

    Private Sub Button_OUT16_Click(sender As Object, e As EventArgs) Handles Button_OUT16.Click
        Button_Change(16)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT17_Click(sender As Object, e As EventArgs) Handles Button_OUT17.Click
        Button_Change(17)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT18_Click(sender As Object, e As EventArgs) Handles Button_OUT18.Click
        Button_Change(18)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT19_Click(sender As Object, e As EventArgs) Handles Button_OUT19.Click
        Button_Change(19)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT20_Click(sender As Object, e As EventArgs) Handles Button_OUT20.Click
        Button_Change(20)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT21_Click(sender As Object, e As EventArgs) Handles Button_OUT21.Click
        Button_Change(21)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT22_Click(sender As Object, e As EventArgs) Handles Button_OUT22.Click
        Button_Change(22)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT23_Click(sender As Object, e As EventArgs) Handles Button_OUT23.Click
        Button_Change(23)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT24_Click(sender As Object, e As EventArgs) Handles Button_OUT24.Click
        Button_Change(24)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT25_Click(sender As Object, e As EventArgs) Handles Button_OUT25.Click
        Button_Change(25)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT26_Click(sender As Object, e As EventArgs) Handles Button_OUT26.Click
        Button_Change(26)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT27_Click(sender As Object, e As EventArgs) Handles Button_OUT27.Click
        Button_Change(27)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT28_Click(sender As Object, e As EventArgs) Handles Button_OUT28.Click
        Button_Change(28)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT29_Click(sender As Object, e As EventArgs) Handles Button_OUT29.Click
        Button_Change(29)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT30_Click(sender As Object, e As EventArgs) Handles Button_OUT30.Click
        Button_Change(30)
        IO_SET(1, 16)
    End Sub

    Private Sub Button_OUT31_Click(sender As Object, e As EventArgs) Handles Button_OUT31.Click
        Button_Change(31)
        IO_SET(1, 16)
    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            If Robot_Connected = False Then
                GoTo d
            End If
            IO_Read("1")
            waitstandby(200)
            Dim buffer As String = ""
            Dim IO_buffer As String = ""
            For i = 0 To 3
                Dim ss As String = Receive_IOValue(i)
                Dim k As Integer = 3
a:
                If k >= 0 Then
                    buffer = HXtoB(ss(k))
                    IO_buffer += buffer(3) & ","
                    IO_buffer += buffer(2) & ","
                    IO_buffer += buffer(1) & ","
                    IO_buffer += buffer(0) & ","
                    k -= 1
                    GoTo a
                End If

            Next

            Dim sss() As String
            sss = IO_buffer.Split(",")
            For i = 0 To 31
                InputIO_Value(i) = sss(i)
            Next
            For i = 32 To 63
                OutputIO_Value(i - 32) = sss(i)
            Next

            For i = 0 To 31
                If InputIO_Value(i) = 0 Then
                    IO_Input(i).Text = "OFF"
                    IO_Input(i).BackColor = Color.WhiteSmoke
                    IO_Input(i).UseVisualStyleBackColor = True
                Else
                    IO_Input(i).Text = "ON"
                    IO_Input(i).BackColor = Color.Lime
                    IO_Input(i).UseVisualStyleBackColor = False
                End If

                If OutputIO_Value(i) = 0 Then
                    IO_Output(i).Text = "OFF"
                    IO_Output(i).BackColor = Color.WhiteSmoke
                    IO_Output(i).UseVisualStyleBackColor = True
                Else
                    IO_Output(i).Text = "ON"
                    IO_Output(i).BackColor = Color.Lime
                    IO_Output(i).UseVisualStyleBackColor = False
                End If
            Next

            'If gPriority = 1 Then
            '    Button_IOcontrol.Enabled = True
            '    Button1.Enabled = True
            'Else
            '    Button_IOcontrol.Enabled = False
            '    Button1.Enabled = False
            '    Button_Enable("disable")
            'End If
d:
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If Button1.Text = "修改标签" Then
                Timer1.Enabled = False
                Button1.Text = "保存"
                For I = 0 To 31
                    TEXT_IN(I).ReadOnly = False
                    TEXT_OUT(I).ReadOnly = False
                Next

            Else
                Dim file_num As Integer = FreeFile()
                If Exists(filePath) Then
                    Kill(filePath)
                End If
                FileOpen(file_num, filePath, OpenMode.Append)
                For i = 0 To 31
                    '存储文件
                    Print(file_num, TEXT_IN(i).Text & vbCrLf)

                Next
                For i = 0 To 31
                    '存储文件
                    Print(file_num, TEXT_OUT(i).Text & vbCrLf)
                Next
                FileClose(file_num)
                MsgBox("保存成功！")
                Timer1.Enabled = True
                Button1.Text = "修改标签"
                For I = 0 To 31
                    TEXT_IN(I).ReadOnly = True
                    TEXT_OUT(I).ReadOnly = True
                Next
            End If


        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Public Sub IO_SET(ByVal RBID As String, ByVal StartNO As Integer)
        Dim sSendData As String = ""
        Dim lStatus As Integer
        Dim CommandCode As Integer = 215
        Dim Value(4) As Long
        'IO_ValueHx=
        'Data
        For i = 0 To 3
            Value(0) += OutputIO_Value(i + StartNO) * (2 ^ (i))
            Value(1) += OutputIO_Value(i + 4 + StartNO) * (2 ^ (i))
            Value(2) += OutputIO_Value(i + 8 + StartNO) * (2 ^ (i))
            Value(3) += OutputIO_Value(i + 12 + StartNO) * (2 ^ (i))
        Next

        IO_ValueHx = Hex(Value(3)).ToString & Hex(Value(2)).ToString & Hex(Value(1)).ToString & Hex(Value(0)).ToString

        sSendData = StartNO.ToString & vbCrLf & IO_ValueHx


    End Sub
    Public Sub IO_Read(ByVal RBID As String)
        '214
        Dim sSendData As String = ""
        Dim lStatus As Integer
        Dim CommandCode As Integer = 214
        ' sSendData = "2<LF>" & "0<LF>" & "1<LF>" & "2<LF>" & "0<LF>" & "1"
        sSendData = "2" & vbCrLf & "0" & vbCrLf & "1" & vbCrLf & "2" & vbCrLf & "0" & vbCrLf & "1"

       
    End Sub

    Private Sub Button_IOenable_Click(sender As Object, e As EventArgs) Handles Button_IOcontrol.Click
        Try
            If Button_IOcontrol.Text = "开启IO控制" Then
                Button_IOcontrol.Text = "关闭IO控制"
                Button_Enable("enable")
            Else
                Button_Enable("disable")
                Button_IOcontrol.Text = "开启IO控制"
            End If
        Catch ex As Exception

        End Try
    End Sub

End Class